package service;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import utils.DaoUtil;
import beans.orderdemo.OrderQuery;
import domain.Order;

/**
 * 订单业务处理服务对象.
 * @author spring
 */
public class OrderService {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(OrderService.class);

    /** Hibernate SessionFactory. */
    private SessionFactory sessionFactory;

    /**
     * 查询订单信息.
     * @param orderQuery 查询对象
     */
    public void queryOrders(OrderQuery orderQuery) {
        DaoUtil.doPagedQuery(sessionFactory.getCurrentSession(), orderQuery);

    }

    /**
     * set session factory
     * @param pSessionFactory factory
     */
    public void setSessionFactory(SessionFactory pSessionFactory) {
        sessionFactory = pSessionFactory;
    }

    /**
     * @param pIds pIds
     */
    public void delOrderByIDs(Integer[] pIds) {
        Session session = sessionFactory.getCurrentSession();

        String hsql = "delete from Order where id in(:id)";
        int count = session.createQuery(hsql).setParameterList("id", pIds)
                .executeUpdate();
        logger.info("delete orders count " + count);

    }

    /**
     * 取得某个订单的信息.
     * @param id ID
     * @return Order
     */
    public Order getOrderById(Integer id) {
        return (Order) sessionFactory.getCurrentSession().get(Order.class, id);
    }
    
    /**
     * @param order 要添加的订单.
     */
    public void addOrder(Order order) {
        sessionFactory.getCurrentSession().save(order);
        logger.info("created new order " + order.getId());

    }

    /**
     * @param order 要修改的订单.
     */
    public void updateOrder(Order order) {
        sessionFactory.getCurrentSession().update(order);
        logger.info("update order " + order.getId());
    }

}
